var express = require('express');
var pg = require('pg');
var fs = require('fs');
var app = express();

const port = 3006;
const basePath = '/home/pi/disk/Pi/downloads/'

app.get('/api/video/list', function (req, res){
  fs.readdir(basePath, function(error, files) {
    res.json({
      code: 1,
      message: '',
      data: files
    })
  })
});

app.get('/play', function (req, res){
  const name = req.query.name;
  res.writeHead(200, {'Content-Type': 'video/mp4'});
  var rs = fs.createReadStream(basePath + name);
  rs.pipe(res);
  rs.on('end', function () {
      res.end();
      console.log('end call');
  });
});

app.get('/', function (req, res){
  res.json({
      code: 1,
      message: '没开发呢！',
      data: {}
    })
});

var server = app.listen(port, function(){
  
  console.log("应用运行中..... port:", port);
});
